package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.CheckException;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;

import java.util.*;

import com.sky.vo.DishVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * DishService
 *
 * @author liudo
 * @version 1.0
 * @project sky-take-out
 * @date 2024/2/4 10:49:16
 */
@Service
@RequiredArgsConstructor
public class DishServiceImpl implements DishService {

    private final DishMapper dishMapper;

    private final DishFlavorMapper dishFlavorMapper;


    @Override
    @Transactional
    public void create(DishDTO dishDTO) {
        // 1. 参数校验
        // 校验名称合法性
        String name = dishDTO.getName();
        if (Objects.isNull(name) || name.length() < 2 || name.length() > 20) {
            throw new CheckException(MessageConstant.DISH_NAME_ERROR);
        }
        Dish dishQuery = new Dish();
        dishQuery.setName(name);
        Integer count = dishMapper.selectCount(dishQuery);
        if (count > 0) {
            throw new CheckException(MessageConstant.DISH_NAME_EXIST);
        }
        // ... 其他校验省略

        // 2. 核心业务的处理
        // 1. 菜品数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.insert(dish);
        // 2. 多个口味数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (Objects.nonNull(flavors) && !flavors.isEmpty()) {
            // 保存多个口味
            // 批量的保存
            flavors.forEach(item -> item.setDishId(dish.getId()));
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        // 1. 参数校验
        int page = Objects.isNull(dishPageQueryDTO.getPage()) ? 1 : dishPageQueryDTO.getPage();
        int pageSize = Objects.isNull(dishPageQueryDTO.getPageSize()) ? 10 : dishPageQueryDTO.getPageSize();

        PageHelper.startPage(page, pageSize);

        Dish dishQuery = new Dish();
        BeanUtils.copyProperties(dishPageQueryDTO, dishQuery);
        List<Dish> dishes = dishMapper.selectList(dishQuery);
        Page<Dish> dishPage = (Page<Dish>) dishes;

        PageResult pageResult = new PageResult();
        pageResult.setTotal(dishPage.getTotal());
        pageResult.setRecords(dishPage.getResult());

        return pageResult;
    }

    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {
        // 怎么保证所有的菜都下架 - 当IDs中只要存一个上架的就不行
        Integer count = dishMapper.selectCountByIdsAndStatus(ids, StatusConstant.ENABLE);
        if (count > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        }
        // 批量删除菜品
        dishMapper.deleteByIds(ids);
        // 批量删除口味
        dishFlavorMapper.deleteByDishIds(ids);
    }

    @Override
    public DishVO infoById(Long id) {
        // 菜品的信息
        Dish dish = dishMapper.selectById(id);
        // 口味的信息
        List<DishFlavor> dishFlavors = dishFlavorMapper.selectByDishId(id);

        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    @Override
    @Transactional
    public void update(DishDTO dishDTO) {
        // 1. 参数校验
        // 校验名称合法性
        String name = dishDTO.getName();
        if (Objects.isNull(name) || name.length() < 2 || name.length() > 20) {
            throw new CheckException(MessageConstant.DISH_NAME_ERROR);
        }
        Dish dishQuery = new Dish();
        dishQuery.setName(name);
        dishQuery.setId(dishDTO.getId());
        Integer count = dishMapper.selectCount(dishQuery);
        if (count > 0) {
            throw new CheckException(MessageConstant.DISH_NAME_EXIST);
        }
        // 菜品修改
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.updateById(dish);
        // 口味的修改
        dishFlavorMapper.deleteByDishIds(Collections.singletonList(dish.getId()));
        // 2. 多个口味数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (Objects.nonNull(flavors) && !flavors.isEmpty()) {
            // 保存多个口味
            // 批量的保存
            flavors.forEach(item -> item.setDishId(dish.getId()));
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    @Override
    public List<Dish> listByCategoryId(Long categoryId) {
        return dishMapper.selectListByCategoryId(categoryId);
    }
}
